import Vue from 'vue'
import router from './router'
// 子应用配置
export const MICRO_CONF = [
    {
        name: '微前端1',
        entry: '//localhost:8081/',
        container: '#qiankunID',
        activeRule: '/app1',
        props:
            {
                routerBase:'/app1',
                msg:'到子应用的消息',
                activeIndex:store.state.activeIndex,

            },

    }
]

start();

// 手动控制应用加载
//import {loadMicroApp} from 'qiankun';
import { loadMicroApp, start,initGlobalState } from 'qiankun';
const globalActions = initGlobalState({ isLogout: false })
Vue.prototype.$globalActions = globalActions
// 缓存应用实例
const microList = new Map([])
// 当前应用配置
let current
router.beforeEach( async (to, from, next) =>{
    const conf = MICRO_CONF.find(item => to.path.indexOf(item.activeRule) !== -1)
    // 应用跳转
    if(conf){
        // 未切换子应用
        if(current &&  current.activeRule === conf.activeRule ){
            next()
            return
        }
        const cacheMicro = microList.get(conf.activeRule)
        // 已缓存应用
        if(cacheMicro){
            next()
            return
        }
        // 未缓存应用
        const micro = loadMicroApp({...conf, router})
        microList.set(conf.activeRule, micro)
        current = conf
        next()
    }
    // 主应用内跳转
    next()
})





